define(['jquery', 'bootstrap', 'backend', 'table', 'form','layui'], function ($, undefined, Backend, Table, Form,layui) {

    var Controller = {

        index: function () {

            layui.use(['element','layer','form'], function() {

                var element = layui.element;
                var layer = layui.layer,form = layui.form
                $ = layui.$;
                var html = "<option value='1'>全部酒店</option>";

                var hid=1;
                var starttime = "";
                var endtime = "";

                //初始化 获取下拉酒店数据
                Fast.api.ajax({
                    url:'hotels/getHotels',
                }, function(data,ret){
                    //成功的回调
                    if(ret.code == 1){
                        $.each(ret.rows,function (i,val) {
                            html +='<option value='+ val["hid"] +'>'+ val["hoteltitle"] +'</option>';
                        })
                        $("#sel").append(html);
                        //重新渲染select 否则数据不会显示
                        form.render("select");
                        //等数据样书都加载完成后，再显示，否则样书不全不好看
                        $("#seldiv").show();
                        $("#btndiv").show();
                        $("#roombody").show();
                    }
                    return false;
                }, function(data,ret){
                    console.log(ret)
                    return false;
                });

                //初始化 首页数据
                getData(hid,starttime,endtime);
                //获取房间数据
                function getData(hotelId,starttime,endtime){
                    Fast.api.ajax({
                        url:'creditor/ajaxIndex',
                        data:{hid:hotelId,starttime:starttime,endtime:endtime}
                    }, function(data, ret){
                        //成功的回调
                        if(ret.code == 1){
                            //筛选出的id集合
                            //总收入
                            $(".all_income").html("¥"+ret.rows.income.all_income)
                            //普通商家应收
                            $(".shop_all_income").html("¥"+ret.rows.income.shop_all_income)
                            //现金
                            $("#xianjin").html("¥"+ret.rows.income.xianjin)
                            $(".wx_income_ids").attr("ids",ret.rows.list.wx_list);
                            $("#wx_income_sum").html("¥"+ret.rows.list.wx_sum);
                            //普通商家小时数
                            $(".shop_hour").html(""+ret.rows.income.shop_all_hour)
                            $(".shop_hour_list").attr("ids",ret.rows.list.shop_all_ids)

                            //充值
                            $("#cz_income").html("¥"+ret.rows.income.cz_income);
                            $("#cz_income_ids").attr("ids",ret.rows.list.cz_list);

                            //小时卡
                            $("#card_imcome").html("¥"+ret.rows.income.card_imcome)
                            $("#xsk_income_ids").attr("ids",ret.rows.list.xsk_list);
                            $("#xsk_income_sum").html("¥"+ret.rows.list.xsk_sum);

                            //退款
                            $(".tk_imcome").html("¥"+ret.rows.income.tk_imcome)
                            $(".tk_income_ids").attr("ids",ret.rows.list.tk_list);


                        }
                        return false;


                    }, function(data, ret){
                        $("#roombody").empty()

                        return false;
                    });
                }

                //切换下拉数据
                form.on('select(selecthotel)', function(data){
                    hid = data.value;
                    getData(hid,starttime,endtime);

                });

                //首页搜索
                $(document).on("click", ".btn-search", function () {
                    starttime = $("#starttime").val()
                    endtime = $("#endtime").val()

                    if(starttime && endtime){
                        if(endtime <= starttime){
                            layer.alert("结束日期必须大于开始日期");
                            return
                        }

                    }

                    getData(hid,starttime,endtime);
                    return

                });


            });

            //自定义事件 修改btn颜色
            $(document).on("click", "#btndiv .layui-input-block div", function () {
                var index = $(this).index();
                if($(this).hasClass("layui-form-checked")){

                    if(index == 1){
                        console.log("空闲")
                        //空闲
                        $(this).children("span").css("background","#5FB878");
                        $(".diy_normal").css("display","inline-block");
                    }else if(index == 3){
                        //使用
                        $(this).children("span").css("background","#1E9FFF");
                        $(".diy_using").css("display","inline-block");
                    }else if(index == 5){
                        //预定
                        $(this).children("span").css("background","#FFB800");
                        $(".diy_reserve").css("display","inline-block");
                    }else if(index == 7){
                        //打扫
                        $(this).children("span").css("background","mediumvioletred");
                        $(".diy_cleaning").css("display","inline-block");
                    }else if(index == 9){
                        //维修
                        $(this).children("span").css("background","#FF5722");
                        $(".diy_repair").css("display","inline-block");
                    }else if(index == 11){
                        //隐藏
                        $(this).children("span").css("background","#393D49");
                        $(".diy_hidden").css("display","inline-block");
                    }
                }else{

                    if(index == 1){
                        //空闲
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_normal").css("display","none");
                    }else if(index == 3){
                        //使用
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_using").css("display","none");
                    }else if(index == 5){
                        //预定
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_reserve").css("display","none");
                    }else if(index == 7){
                        //打扫
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_cleaning").css("display","none");
                    }else if(index == 9){
                        //维修
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_repair").css("display","none");
                    }else if(index == 11){
                        //隐藏
                        $(this).children("span").css("background","#d2d2d2");
                        $(".diy_hidden").css("display","none");
                    }


                    //$(this).children("span").css("background","#d2d2d2");

                }
            });


            $(document).on("click","#hideright",function () {
                var sideBar = $("#sideBar");
                $("#sideBar").removeClass("addWidth");
                $("#roombody").css("width","100%");

            })


            //自定义弹出列表窗口 jsw 2021-08-12 明细弹窗 修改

            $(document).on("click",".incomediv",function () {
                var type = $(this).attr("type")
                var ids = $(this).attr("ids")


                if(type == "xianjin"){
                    var titlle = "订单明细"
                }else if(type == "chongzhi"){
                    var titlle = "充值记录"
                    var url = 'chongzhi?ids='+ids;//弹出窗口 add.html页面的（fastadmin封装layer模态框将以iframe的方式将add输出到index页面的模态框里）
                    Fast.api.open(url, titlle, {
                        'area':[
                            $(window).width() > 800 ? '80%' : '800',
                            $(window).height() > 600 ? '800px' : '80%'
                        ],
                    });
                    return;

                }else if(type == "xiaoshika"){
                    var titlle = "小时卡购买记录"

                    var url = 'user_card?ids='+ids;//弹出窗口 add.html页面的（fastadmin封装layer模态框将以iframe的方式将add输出到index页面的模态框里）
                    Fast.api.open(url, titlle, {
                        'area':[
                            $(window).width() > 800 ? '80%' : '800',
                            $(window).height() > 600 ? '800px' : '80%'
                        ],
                    });
                    return;

                }else if(type == "tuikuan"){
                    var titlle = "退款订单明细"
                }

                var url = 'cash?type='+ type +'&id='+ids;//弹出窗口 add.html页面的（fastadmin封装layer模态框将以iframe的方式将add输出到index页面的模态框里）
                Fast.api.open(url, titlle, {
                    'area':[
                        $(window).width() > 800 ? '98%' : '800',
                        $(window).height() > 600 ? '800px' : '100%'
                    ],
                });

            })



            //显示右侧面板
            function showRightWall(id){
                var sideBar = $("#sideBar");
                //我的逻辑上是先判断没有这个class，没有的话就添加，这个添加动作会触发宽度加长的过渡效果
                if (!sideBar.hasClass("addWidth")) {
                    $("#sideBar").addClass("addWidth");
                    $("#roombody").css("width","80%");
                }
                //同理，展开之后要切换，移除这个class来触发减小宽度的过渡效果
                else {
                    //$("#sideBar").removeClass("addWidth");
                    //$("#roombody").css("width","100%");
                }

                $.ajax({
                    type: "GET",
                    url: "rooms/getRoomInfo",
                    data: {id:id},
                    dataType: "json",
                    success: function(data){

                        var datainfo = data.data;
                        $("#statusid").val(datainfo.id)
                        $("#statushtitle").val(datainfo.htitle)
                        $("#statustitle").val(datainfo.h_title)
                        $("#statuscode").val(datainfo.hstorey + datainfo.h_unit +" "+ datainfo.h_housebrand)
                        $("#statusprice").val(datainfo.h_price)
                        $("#statusallprice").val(datainfo.h_allprice)
                        $("#beforeTime").val(datainfo.beforeTime)

                        if(datainfo.h_statusText == "reserve"){
                            $("#reserveStatus").css("display","block");
                        }else{
                            $("#reserveStatus").css("display","none");
                        }

                        //动态设置select 选项值
                        var select = 'dd[lay-value=' + datainfo.h_statusText + ']';
                        $('#statusText').siblings("div.layui-form-select").find('dl').find(select).click();

                        var moveInfo = data.data.moveInfo; //入住信息
                        if(moveInfo == ''){
                            $("#moveIntoInfo").css("display","none");
                        }else{
                            //渲染信息
                            $("#moveIntoInfo .classAddtime").html(moveInfo.add_time)
                            $("#moveIntoInfo .classUser").html(moveInfo.nickName)
                            $("#moveIntoInfo .classStime").html(moveInfo.stime)
                            $("#moveIntoInfo .classMtime").html(moveInfo.moveinto_time)
                            $("#moveIntoInfo .classEtime").html(moveInfo.etime)
                            $("#moveIntoInfo .classAllmoney").html(moveInfo.all_money)
                            $("#moveIntoInfo").css("display","block");
                        }

                        var reserveList = data.data.reserveList; //预订信息
                        $("#reserveInfo").empty();//先清空预约数据
                        var relen = reserveList.length;

                        var reservehtml = "";
                        var selectHtml = "";
                        if(relen > 0){
                            for(var i= 0;i<relen;i++){

                                reservehtml += '<ul style="position: relative"><div class="userselect" style="position: absolute;left: -40%;top: 45%;" ><input type="radio" name="user_ordernumber" value="'+ reserveList[i]["order_number"] +'" ';
                                if(i == 0){
                                    reservehtml += "checked";
                                }

                                reservehtml += '> </div>\n' +
                                    '                                                    <li>订单时间：<span>'+ reserveList[i]["add_time"] +'</span></li>\n' +
                                    '                                                    <li>预订用户：<span>'+ reserveList[i]["nickName"] +'</span></li>\n' +
                                    '                                                    <li>预订时间：<span class="classStime">'+ reserveList[i]["stime"] +'</span></li>\n' +
                                    '                                                    <li>入住时间：<span>'+ reserveList[i]["moveinto_time"] +'</span></li>\n' +
                                    '                                                    <li>退房时间：<span class="classEtime">'+ reserveList[i]["etime"] +'</span></li>\n' +
                                    '                                                    <li>订单状态：<span>预订中</span></li>\n' +
                                    '                                                    <li>已缴费用：<span>'+ reserveList[i]["all_money"] +'</span>元</li>\n' +
                                    '\n' +
                                    '                                                </ul>\n' +
                                    '                                                <hr>';
                            }


                            //渲染入住人员信息
                            selectHtml += ' <option value="using">人员二</option>';



                        }

                        $(".user_ordernumber:first").attr("checked",true);
                        $("#reserveInfo").append(reservehtml);
                        $("#reservePeople").append(selectHtml);

                        layui.use(['element','layer','form'], function() {

                            var form = layui.form;


                            form.render("radio");
                            form.render("select");

                        });








                    }
                });

            }

            //修改状态
            $("#changeStatusText").click(function () {
                var hotelid = $("#sel").val();
                var id = $("#statusid").val();
                var statusText = $("#statusText").val();
                var order_number  = $("input[name='user_ordernumber']:checked").val();

                layer.confirm('是否确定修改房间状态?', {icon: 3, title:'状态更新'}, function(index){

                    $.ajax({
                        type: "GET",
                        url: "rooms/changeRoomStatus",
                        data: {id:id,statusText:statusText,order_number:order_number},
                        dataType: "json",
                        success: function(data){
                            if(data.code == 1){
                                layer.msg(data.msg,{time:2000,icon: 6})

                                //关闭右侧
                                var sideBar = $("#sideBar");
                                $("#sideBar").removeClass("addWidth");
                                $("#roombody").css("width","100%");
                            }else{
                                layer.msg(data.msg,{time:2000,icon: 5})
                                return false;
                            }

                        }
                    });

                    layer.close(index);
                });

            })

            //首页日期渲染
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD HH:mm:ss',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });

            //首页刷新
            $(document).on("click", ".btn-refresh", function () {
                window.location.reload();//刷新
            });


        },

    };
    return Controller;
});
